package com.sskj.mine.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.AddressList;
import com.sskj.mine.bean.WalletAddressBean;
import com.sskj.mine.bean.rxbus.AddressBean;
import com.sskj.mine.presenter.WalletAddressManagerActivityPresenter;
import com.sskj.mine.util.TipMineUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.MINE_WALLET_ADDRESS_MANAGER)//钱包地址管理
public class WalletAddressManagerActivity extends BaseActivity<WalletAddressManagerActivityPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.line_bottom)
    View line_bottom;
    @Autowired
    boolean isWithDraw;
    private SlimAdapter<WalletAddressBean> slimAdapterOut;
    private String clickId;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_wallet_address_manager;
    }

    @Override
    public WalletAddressManagerActivityPresenter getPresenter() {
        return new WalletAddressManagerActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.mineWalletAddressManagerActivity0));
        RxBus.getDefault().register(this);
        ARouter.getInstance().inject(this);
        List<WalletAddressBean> walletAddressBeans = new ArrayList<>();
        walletAddressBeans.add(new WalletAddressBean("比特币系列", "1"));
        walletAddressBeans.add(new WalletAddressBean("以太坊系列", "2"));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterOut = SlimAdapter.create().register(R.layout.mine_recy_item_wallet_manager_out, new SlimInjector<WalletAddressBean>() {
            @Override
            public void onInject(WalletAddressBean data, IViewInjector injector, List payloads) {
                injector
                        .text(R.id.tvTitle, data.getCode())
                        .clicked(R.id.ivAdd, v -> {
                            ARouter.getInstance()
                                    .build(RConfig.MINE_WALLET_ADDRESS_ADD)
                                    .withString(Constans.CODE, data.getCode())
                                    .withString(Constans.TYPE, data.getType())
                                    .navigation();
                        });
                List<AddressList.EthBean> ethBeans;
                if (data.getList() == null || data.getList().size() == 0) {
                    ethBeans = new ArrayList<>();
                } else {
                    ethBeans = data.getList();
                }
                RecyclerView recyclerViewInner = (RecyclerView) injector.findViewById(R.id.recyclerView);
                recyclerViewInner.setLayoutManager(new LinearLayoutManager(WalletAddressManagerActivity.this));
                recyclerViewInner.addItemDecoration(new DividerLineItemDecoration(WalletAddressManagerActivity.this)
                        .setFirstDraw(false)
                        .setLastDraw(false)
                        .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 40))
                        .setRightPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 40))
                        .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.mine_525869))
                        .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1)));
                SlimAdapter<AddressList.EthBean> slimAdapterInner = SlimAdapter.create().register(R.layout.mine_recy_item_wallet_manager, new SlimInjector<AddressList.EthBean>() {
                    @Override
                    public void onInject(AddressList.EthBean data, IViewInjector injector, List payloads) {
                        injector
                                .text(R.id.tvRemark, data.getNotes())
                                .text(R.id.tvAddress, data.getAddress())
                                .clicked(R.id.tvDelete, v -> {
                                    clickId = data.getId();
                                    deleteWallet();
                                });

                    }
                }).attachTo(recyclerViewInner).updateData(ethBeans);
                ItemClickSupport.addTo(recyclerViewInner)
                        .setOnItemClickListener((recyclerView, position, v) -> {
                            if (isWithDraw) {
                                RxBus.getDefault().post(new AddressBean(slimAdapterInner.getDataItem(position).getAddress()));
                                finish();
                            }
                        });
            }
        }).attachTo(recyclerView).updateData(walletAddressBeans);
        mPresenter.getWalletList();
    }

    @Subscribe(code = RxBusCode.WALLET_LIST_REFRESH, threadMode = ThreadMode.MAIN)
    public void refresh() {
        if (mPresenter != null) {
            mPresenter.getWalletList();
        }
    }

    private void deleteWallet() {
        TipMineUtil.getTip(this, App.INSTANCE.getString(R.string.mineWalletAddressManagerActivity1), () -> {
            mPresenter.delWalletAddress(clickId);
        }).show();
    }

    public void updateUI(AddressList data) {

        slimAdapterOut.getData().get(0).setList(data.getBtc());
        slimAdapterOut.getData().get(1).setList(data.getEth());
        line_bottom.setVisibility(data.getEth().size() > 0 ? View.VISIBLE : View.GONE);
//        slimAdapterOut.getData().get(2).setList(data.getEth());
//        slimAdapterOut.getData().get(3).setList(data.getBch());
//        slimAdapterOut.getData().get(4).setList(data.getLtc());
        slimAdapterOut.notifyItemRangeChanged(0, 2);
    }

    @Override
    protected void onDestroy() {
        RxBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
